package top.healthylife.gateway.platform.kjt.api.util;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.basics.utils.utils.SystemThreadLocal;
import top.healthylife.gateway.core.util.SystemConfigUtils;
import top.healthylife.gateway.dto.request.req.ApiReqDto;
import top.healthylife.gateway.platform.kjt.api.config.SystemConfig;
import top.healthylife.gateway.platform.kjt.api.dto.KjtReqDto;
import top.healthylife.gateway.platform.kjt.api.secret.Core;
import top.heathylife.gateway.common.cons.SystemCons;
import top.heathylife.gateway.common.po.RecordRequestInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


@Slf4j
public class KjtPostUtil {
    /**
     * 请求快捷通具体实现方法
     * @param kjtReqDto
     * @return
     * @throws Exception
     */
    public static <T> T selectKjt(KjtReqDto kjtReqDto, SystemConfig systemConfig, Class<T> clz) throws Exception{
        final String charset = "UTF-8";
        String serviceName = "four_factor_bank_auth";
        final String version = "1.0";
//        final String partnerId = "200003198419";
        if (!Objects.isNull(kjtReqDto.getCheck()) && kjtReqDto.getCheck() == 3) {
            serviceName = "three_factor_bank_auth";
        }
        // 快捷通生产环境
        String requestUrl = "https://mgs.kjtpay.com/mgs/recvOrder.htm";
        Map<String, String> param = new HashMap<>(16);
        param.put("service", serviceName);
        param.put("version", version);
        // 商户号
        param.put("partner_id", systemConfig.getPartnerId());
        param.put("_input_charset", charset);
        param.put("card_no", kjtReqDto.getBankNo());
        param.put("account_name", kjtReqDto.getName());
        param.put("mobile", kjtReqDto.getPhone());
        param.put("id_card", kjtReqDto.getIdNo());

        param.put("outer_order_no", String.valueOf(SystemConfigUtils.getNo()));

        log.info("处理前请求参数： " + param.toString());
        // 参数处理C
        param = Core.buildRequestPara(param, "RSA", systemConfig.getPrivateKey());
        log.info("处理后请求参数： " + param.toString());
        RecordRequestInfo.recordRequest(requestUrl, JSONObject.toJSONString(param), null, "post");
        String post = HttpUtil.post(requestUrl, JSONObject.parseObject(JSONObject.toJSONString(param)));
        log.info("请求快捷通结果为：{}",post);
        //将三方元数据存储到线程变量
        SystemThreadLocal.set(SystemCons.ORIGIN_DATA,post);
        String result = splitStr(post);
        String respCode = JSONObject.parseObject(result).getString("resp_code");
        //返回状态码为S0001或D0048则为正常返回数据
        if ("S0001".equals(respCode)||"D0048".equals(respCode)){
        return JSONObject.parseObject(result,clz);
        }
        throw new BusinessException("快捷通返回结果异常,result:"+result);
    }

    /**
     * 处理快捷通返回结果
     * @param responseStr   返回数据
     * @return
     */
    private static String splitStr(String responseStr) {
        String[] strings = responseStr.split("&");
        Map<String,String> map = new HashMap<>(16);
        for (int i = 0; i < strings.length; i++) {
            String[] split = strings[i].split("=");
            map.put(split[0],split[1]);
        }
        return JSONObject.toJSONString(map);
    }

    /**
     * 访问日志输出和配置获取
     *
     * @param baofuReqDeo 入参
     * @return
     */
    public static SystemConfig systemConfigInfo(ApiReqDto baofuReqDeo) {
        //可直接通过工具类获取这个对象
        SystemConfig o = (SystemConfig) SystemConfigUtils.get();
        log.info("收到请求参数为:{}", JSONObject.toJSONString(baofuReqDeo));
        log.info("收到请求参数为:{}", JSONObject.toJSONString(o));
        return o;
    }
}

